package com.sdut.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdut.material.pojo.Manufacture;
import com.sdut.material.mapper.ManufactureMapper;
import com.sdut.material.pojo.ManufactureVO;
import com.sdut.material.pojo.ProductionMaterial;
import com.sdut.material.pojo.vo.DemandVO;
import com.sdut.material.service.IManufactureService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdut.material.service.IOutputService;
import com.sdut.material.service.IProductionMaterialService;
import com.sdut.material.service.IRepositoryService;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Service
public class ManufactureServiceImpl extends ServiceImpl<ManufactureMapper, Manufacture> implements IManufactureService {
    @Autowired
    private ManufactureMapper manufactureMapper;
    @Autowired
    private IProductionMaterialService productionMaterialService;
    @Autowired
    private IRepositoryService repositoryService;
    @Autowired
    private IOutputService outputService;
    @Override
    public LayUITableResult selectByPage(Long page, Long limit) {
        long offset = (page-1)*limit;
        List<ManufactureVO> manufactureVOS = manufactureMapper.selectListPage(offset,limit);
        int countTotal = manufactureMapper.selectTotalCount();
        return LayUITableResult.ok((long)countTotal,manufactureVOS);
    }

    @Override
    public JSONResult insert(String productions, Integer count) {
        String[] productionIds = productions.split(",");
        for (String productionId : productionIds) {
            //根据productionId在production_material表中找到对应的数据
            ProductionMaterial productionMaterial = productionMaterialService.selectByProductionId(Integer.parseInt(productionId));
            System.out.println("productionMaterial: "+productionMaterial);
            //获得产品生产的材料
            int materialId = productionMaterial.getMaterialId();
            System.out.println("materialId"+materialId);
            //获得每件产品所需的材料数
            int materialCount = productionMaterial.getCount();
            System.out.println("materialCount"+materialCount);
            //获得生产总产品的总数量
            int materialTotalCount = materialCount * count;
            System.out.println("materialTotalCount"+materialTotalCount);
            //获得仓库内对应材料的总数量
            int repositorySum = repositoryService.selectSumByMaterialId(materialId);
            System.out.println("repositorySum"+repositorySum);
            //将生产产品的所需要物料,物料所需要的材料数传到出库单
            outputService.transmit(materialId,materialTotalCount);
            Manufacture manufacture = new Manufacture();
            if (repositorySum < materialTotalCount) {
                DemandVO demandVO = new DemandVO();
                int demandCount = materialTotalCount - repositorySum;
                System.out.println("demandCount"+demandCount);
                manufacture.setProductionId(Integer.parseInt(productionId));
                manufacture.setCount(count);
                manufacture.setState(3);
                manufactureMapper.insert(manufacture);
                demandVO.setMaterialId(materialId);
                demandVO.setDemandCount(demandCount);
                return JSONResult.error("材料不足添加失败，是否继续添加",demandVO);
            }
            manufacture.setProductionId(Integer.parseInt(productionId));
            manufacture.setCount(count);
            manufacture.setState(2);
            manufactureMapper.insert(manufacture);
        }
        return JSONResult.ok("添加成功");
    }

    @Override
    public void cancelById(Integer manufactureId) {
        Manufacture manufacture = manufactureMapper.selectById(manufactureId);
        manufacture.setState(3);
        manufactureMapper.updateById(manufacture);
    }
}
